package model;

import entity.Employee;
import entity.Orders;
import entity.OrdersDetails;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Noi
 */
public class HistoryBookTableModel extends AbstractTableModel {

    private ArrayList<OrdersDetails> alOrdersDetailses;
    private manager.Manager manager;

    public HistoryBookTableModel(manager.Manager manager) {
        this.manager = manager;
        this.alOrdersDetailses = new ArrayList<OrdersDetails>();
    }

    public void setAlOrdersDetailses(ArrayList<OrdersDetails> alOrdersDetailses) {
        this.alOrdersDetailses = alOrdersDetailses;
        this.fireTableDataChanged();
    }

    @Override
    public int getRowCount() {
        return this.alOrdersDetailses.size();
    }

    @Override
    public int getColumnCount() {
        return 4;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        
        OrdersDetails details = this.alOrdersDetailses.get(rowIndex);
        Orders orders = this.manager.getOrdersManager().getByOrdersID(details.getOrdersID());
        Employee employee = this.manager.getEmployeeManager().getByEmployeeID(orders.getEmployeeID());
        SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        
        switch (columnIndex) {
            case 0:
                return employee.getEmployeeName();
            case 1:
                return df.format(orders.getIssueDate());
            case 2:
                return df.format(orders.getDueDate());
            case 3:
                if(details.getReturnDate() == null) return "No";
                else return df.format(details.getReturnDate());
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Employee Name";
            case 1:
                return "Issue Date";
            case 2:
                return "Due Date";
            case 3:
                return "Returned";
            default:
                return null;
        }
    }
}
